#!/usr/bin/python
#-*-coding:utf-8-*-

import unittest
from base import *
from protocol import *

class TestCase():
    def __init__(self, cpu):
        self.cpu = cpu
        self.caseSet = []
        self.test_name = "name"
        self.test_version = 'version'
        self.test_pl = 'platform'
        self.test_net = 'network'
        self.test_sum = 0
        self.test_success = 0
        self.test_failed = 0
        self.test_score = 0

    def assertEqual(self, testResult, expectedResult):
        if testResult == expectedResult:
            return {'status':1,'content':str(testResult)+" == "+str(expectedResult)}
        else:
            return {'status':0,'content':str(testResult)+" != "+str(expectedResult)}

    def execStr(self, string):
        try:
            exec ("self.%s()" % string)
        except Exception,e:
            print ("Error is Occur in "+string+"\nError is: "+str(e))

    def execTest(self):
        self.cpu["data"].reportSummary['test_name'] = self.test_name
        self.cpu["data"].reportSummary['test_version'] = self.test_version
        self.cpu["data"].reportSummary['test_pl'] = self.test_pl
        self.cpu["data"].reportSummary['test_net'] = self.test_net
        self.cpu["data"].reportSummary['test_date'] = time.strftime('%Y-%m-%d %X', time.localtime())

        self.cpu["data"].inTest = True
        for key1 in range(len(self.caseSet)):
            for key2 in range(len(self.caseSet[key1])):
                self.test_sum+=1
                self.execStr(self.caseSet[key1][key2])

        self.cpu["data"].reportSummary['test_sum'] = self.test_sum
        self.cpu["data"].reportSummary['test_success'] = self.test_success
        self.cpu["data"].reportSummary['test_failed'] = self.test_failed
        self.cpu["data"].reportSummary['test_score'] = self.test_success * 100 /self.test_sum
        self.cpu["data"].inTest = False

class TestHttpPost_bak(unittest.TestCase):
    def setUp(self):
        self.arg = {}
        self.arg["username"] = ''
        self.arg["password"] = ''
        self.arg["userType"] = ''
        self.arg["deviceId"] = ''
        self.arg["FN_KEY"] = 'AZ-FN-CODEC-KEY'
        self.arg["timestamp"] = getTimestamp()

    def tearDown(self):
        pass

    # 正常账号登录
    def test_normal(self):
        self.arg["username"] = 'fxs00001'
        self.arg["password"] = '123456'
        self.arg["userType"] = '1'
        self.ajaxRequestBody = getToken({'userCode': self.arg["username"], 'password': md5(self.arg["password"]),
                                    'timestamp': str(self.arg["timestamp"]), 'userType': self.arg["userType"],
                                    'deviceId': ''}, self.arg["FN_KEY"])
        self.assertEqual(HttpPost(self.ajaxRequestBody, "114.55.63.81", "80", "/fn/app/login.do"), 1000)

    # 异常请求链接的登录
    def test_abnormal_1(self):
        self.arg["username"] = 'fxs00001'
        self.arg["password"] = '123456'
        self.arg["userType"] = '1'
        self.ajaxRequestBody = getToken({'userCode': self.arg["username"], 'password': md5(self.arg["password"]),
                                    'timestamp': str(self.arg["timestamp"]), 'userType': self.arg["userType"],
                                    'deviceId': ''}, self.arg["FN_KEY"])
        self.assertEqual(HttpPost(self.ajaxRequestBody, "114.00.00.00", "80", "/fn/app/login.do"), -1)

    # 错误请求链接的登录
    def test_abnormal_2(self):
        self.arg["username"] = 'fxs00001'
        self.arg["password"] = '123456'
        self.arg["userType"] = '1'
        self.ajaxRequestBody = getToken({'userCode': self.arg["username"], 'password': md5(self.arg["password"]),
                                    'timestamp': str(self.arg["timestamp"]), 'userType': self.arg["userType"],
                                    'deviceId': ''}, self.arg["FN_KEY"])
        self.assertEqual(HttpPost(self.ajaxRequestBody, "114.55.63.10", "80", "/fn/app/login.do"), 0)

    # 错误端口请求的登录
    def test_abnormal_3(self):
        self.arg["username"] = 'fxs00001'
        self.arg["password"] = '123456'
        self.arg["userType"] = '1'
        self.ajaxRequestBody = getToken({'userCode': self.arg["username"], 'password': md5(self.arg["password"]),
                                    'timestamp': str(self.arg["timestamp"]), 'userType': self.arg["userType"],
                                    'deviceId': ''}, self.arg["FN_KEY"])
        self.assertEqual(HttpPost(self.ajaxRequestBody, "114.55.63.81", "8000", "/fn/app/login.do"), -1)

    # 请求到了错误的文件
    def test_abnormal_4(self):
        self.arg["username"] = 'fxs00001'
        self.arg["password"] = '123456'
        self.arg["userType"] = '1'
        self.ajaxRequestBody = getToken({'userCode': self.arg["username"], 'password': md5(self.arg["password"]),
                                    'timestamp': str(self.arg["timestamp"]), 'userType': self.arg["userType"],
                                    'deviceId': ''}, self.arg["FN_KEY"])
        self.assertEqual(HttpPost(self.ajaxRequestBody, "114.55.63.81", "80", "login.do"), 0)

    # 密码前加入空格的登录
    def test_abnormal_5(self):
        self.arg["username"] = 'fxs00001'
        self.arg["password"] = ' 123456'
        self.arg["userType"] = '1'
        self.ajaxRequestBody = getToken({'userCode': self.arg["username"], 'password': md5(self.arg["password"]),
                                    'timestamp': str(self.arg["timestamp"]), 'userType': self.arg["userType"],
                                    'deviceId': ''}, self.arg["FN_KEY"])
        self.assertEqual(HttpPost(self.ajaxRequestBody, "114.55.63.81", "80", "/fn/app/login.do"), 1000)

    # 错误密码登录
    def test_abnormal_6(self):
        self.arg["username"] = 'fxs00001'
        self.arg["password"] = '123450'
        self.arg["userType"] = '1'
        self.ajaxRequestBody = getToken({'userCode': self.arg["username"], 'password': md5(self.arg["password"]),
                                    'timestamp': str(self.arg["timestamp"]), 'userType': self.arg["userType"],
                                    'deviceId': ''}, self.arg["FN_KEY"])
        self.assertEqual(HttpPost(self.ajaxRequestBody, "114.55.63.81", "80", "/fn/app/login.do"), 1000)

if __name__ == "__main__":
    # 构造测试集
    #suite = unittest.TestSuite()
    #suite.addTest(TestHttpPost("test_normal"))
    #suite.addTest(TestHttpPost("test_abnormal_1"))
    #suite.addTest(TestHttpPost("test_abnormal_2"))
    #suite.addTest(TestHttpPost("test_abnormal_3"))
    #suite.addTest(TestHttpPost("test_abnormal_4"))
    #suite.addTest(TestHttpPost("test_abnormal_5"))
    #suite.addTest(TestHttpPost("test_abnormal_6"))
    # 执行测试
    suite = unittest.TestSuite()
    runner = unittest.TextTestRunner()
    runner.run(suite)
    pass